#pragma once 
#include "Common.hpp"

class Dictionary
{
public:
    Dictionary(const string& filename):_filename(filename)
    {

    }
    void Init(){
        LOG(INFO) << "开始初始化";
        ifstream ifs(_filename);
        if (!ifs.is_open()){
            LOG(ERROR) << _filename << "not found";
            exit(1);
        }
        LOG(INFO) << "开始读取啦";
        string line;
        while(getline(ifs,line)){
            LOG(INFO) << line;
            auto pos = line.find(": ");
            if (pos == string::npos || pos == line.size()-1){
                LOG(ERROR) << "invalid dictionary, exit process";
                exit(1); //在这里暂且直接退出
            }
            string k = line.substr(0,pos);
            string v = line.substr(pos + 2);
            _dictionary[k] = v;
        }
    }
    string Translate(const string& k){
        if (_dictionary.count(k)) {
            return _dictionary[k];
        }
        return "NONE";
    }
    void debug(){
        LOG(INFO) << "开始调试";
        for (auto& kv : _dictionary){
            LOG(DEBUG) << kv.first << ": " << kv.second; 
        }
    }
    ~Dictionary(){
        
    }
private:
    string _filename;
    unordered_map<string,string> _dictionary;
};